package com.whpost.service.tools;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import static java.nio.file.FileSystems.getDefault;

/**
 * @project
 * @Description
 * @Author songwp
 * @Date 2022/8/25 11:07
 * @Version 1.0.0
 **/
public class QRCodeGenerator {

    /*
     * text - 内容
     * width - 二维码宽度
     * height - 二维码高度
     * filePath - 二维码存放位置
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath,String wjm)throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        File tempFile = new File(filePath);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        Path path = getDefault().getPath(filePath+wjm);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

}